﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using VersionComparer.Base;
using VersionComparer.Views;
using VersionComparer.DataModel;
using VersionComparer.DataModel.Xml;
using VersionComparer.Configuration;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading;
using System.Xml;

namespace VersionComparer
{
    static class Program
    {

    
        private const int ATTACH_PARENT_PROCESS = -1;

      
        static void InitializeXml()
        {
            ServiceLocator.Current.Register<MainForm, XmlForm>();
        }

        static void BuildBigXml(string file)
        {
            var writer = XmlWriter.Create(file);
            writer.WriteStartDocument();
            writer.WriteStartElement("bigxml");

            writer.WriteStartElement("list");
            int max = 30000;
            int subitems = 30;
            int errors = 50;
            for (int i = 0; i < max; i++)
            {
                string value = "00000000-0000-0000-0000-" + i.ToString ().PadLeft (12,'0');
                if ((i + 1) % (max / errors) == 0)
                    value = Guid.NewGuid().ToString();
                writer.WriteStartElement("item_" + i.ToString ());
                writer.WriteAttributeString("id", i.ToString());
                writer.WriteAttributeString("value", value);
                writer.WriteAttributeString("value2", value);
                writer.WriteAttributeString("value3", value);
                writer.WriteAttributeString("value4", value);
                for (int j = 0; j < subitems; j++)
                {
                    writer.WriteStartElement(string.Format("subitem_{0}_{1}" , i, j));
                    writer.WriteAttributeString("id", string.Format("subitem_{0}_{1}", i, j));
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Close();
        }

      
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main()
        {
            //BuildBigXml("c:\\temp\\big.xml");
            //BuildBigXml("c:\\temp\\big2.xml");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            InitializeXml();
            MainForm form1 = ServiceLocator.Current.CreateInstance<MainForm>();
            if (form1.RunningAsCommandLine)
                NativeMethods.AttachConsole(ATTACH_PARENT_PROCESS);
            Application.Run(form1);
            if (form1.RunningAsCommandLine)
                NativeMethods.FreeConsole();
            return form1.TotalErrors;
        }

        static Assembly CurrentDomain_ReflectionOnlyAssemblyResolve(object sender, ResolveEventArgs args)
        {
            Assembly ass = Assembly.ReflectionOnlyLoad(args.Name);
            return ass;
        }
    }

}